﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Web.Http;

using ZeusLite.Code;

namespace ZeusLite.Controllers
{
  /// <summary>
  /// HU - Rakomány info
  /// EN - Package information
  /// </summary>
  public class PackageInventoryInfoController : ApiController
  {
    /// <summary>
    /// Package information
    /// </summary>
    /// <param name="page">Page name</param>
    /// <param name="lang">Language code</param>
    /// <param name="token">User ID / Access Token</param>
    /// <param name="code">Package ID</param>
    /// <param name="type">1 or 2</param>
    /// <returns></returns>
    public HttpResponseMessage Get([FromUri] string page, [FromUri] string lang, 
      [FromUri] string token, [FromUri] string code, [FromUri] string type = "")
    {
      WebResult res = new WebResult();

      try
      {
        if ((lang != null) && (lang != ""))
          Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);

        using (SqlConn conn = new SqlConn())
        {
          conn.Open(ConfigurationManager.ConnectionStrings["Database"].ConnectionString, lang);

          // SQL command
          string sql = "EXEC dsZeusPackageInventoryInfo @Token, @Code";

          // Parameters
          ArrayList array = new ArrayList();

          array.Add(new SqlParameter("@Token", token));
          array.Add(new SqlParameter("@Code", code));

          // 
          DataTable table = conn.Execute(sql, array.ToArray());

          // Process result
          for (int i = 0; i < table.Rows.Count; i++)
          {
            ZeusPackage obj = new ZeusPackage();

            obj.Code = table.Rows[i]["Code"].ToString();
            obj.ArticleCode = table.Rows[i]["ArticleCode"].ToString();
            obj.ArticleName = table.Rows[i]["ArticleName"].ToString();
            obj.Expired = ((DateTime)table.Rows[i]["Expired"]).ToString("d");

            if (type == "1")
            {
              if (table.Rows[i]["Quantity1"] != DBNull.Value)
                obj.Quantity = ((decimal)table.Rows[i]["Quantity1"]).ToString("N");

              obj.LocationCode = table.Rows[i]["Location1"].ToString();
            }

            if (type == "2")
            {
              if (table.Rows[i]["Quantity2"] != DBNull.Value)
                obj.Quantity = ((decimal)table.Rows[i]["Quantity2"]).ToString("N");

              obj.LocationCode = table.Rows[i]["Location2"].ToString();
            }

            obj.UnitCode = table.Rows[i]["UnitCode"].ToString();
            obj.Notes = table.Rows[i]["Notes"].ToString();

            res.Result.Add(obj);
          }

          conn.Close();
        }
      }
      catch (Exception ex)
      {
        res.ErrorCode = 1;
        res.ErrorText = ex.Message;
      }

      // JSON formatted result
      var formatter = new JsonMediaTypeFormatter();

      return Request.CreateResponse(HttpStatusCode.OK, res, formatter);
    }

  }
}
